package org.example.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.damai.entity.DmProduct;
import com.yc.damai.mapper.DmProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/6/23
 * Email haijunzhou@hnit.edu.cn
 */
@RestController
public class ProductDo {

    @Autowired
    DmProductMapper productMapper;

    // 查询首页商品: 热卖 + 最新
    @RequestMapping("queryByIndex")
    public Map<String,Object> queryByIndex(){
        // 热卖
        LambdaQueryWrapper<DmProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DmProduct::getIsHot, 1);
        Page<DmProduct> p = new Page<>(1, 10);
        Page<DmProduct> hots = productMapper.selectPage(p, lqw);
        // 最新
        p.addOrder(OrderItem.desc("id"));
        Page<DmProduct> news = productMapper.selectPage(p, null);

        Map<String,Object> ret = new HashMap<>();
        ret.put("hots",hots.getRecords());
        ret.put("news",news.getRecords());

        return ret;
    }

}
